package com.yzg.cas;

import lombok.extern.slf4j.Slf4j;
import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author misterWei
 * @create 2020年05月24号:13点08分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.AtomicApi")
@sun.misc.Contended
public class AtomicApi {

    public static void main(String[] args) {
        AtomicInteger a = new AtomicInteger(0);
        log.info("先获取在修改 i++ 操作;值为\t{}", a.getAndAdd(1)); //0
        log.info("先获取在修改 ++i 操作;值为\t{}", a.addAndGet(1)); //2

        //递增
        log.info("{}",a.incrementAndGet()); //3    // ++i

        log.info("{}",a.getAndIncrement());//4 // i++

        //递减
        a.decrementAndGet(); // 3 // --i
        a.getAndDecrement(); //2 // i --

        //原子性操作修改
        a.updateAndGet(value -> -1 * value); // -2  // 先更改在返回返回值
        a.getAndUpdate(value -> (value * -1));// 2 //首先返回当前值,在更改;
        log.info("{}",a.get());//2

        /**
         * 
         */
        log.info("{}", a.accumulateAndGet(10,(prev,next) -> prev + next ));//12
        log.info("{}", a.getAndAccumulate(10,(prev,next) -> prev + next ));//12
        log.info("{}",a.get());
        Unsafe unsafe = Unsafe.getUnsafe();

    }
}
